﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Data;

namespace Quartz.WebManager.Logic
{
    internal static class Extensions
    {
        public static void AddParameter(this IDbCommand reference, string name, object value)
        {
            IDbDataParameter parameter = reference.CreateParameter();
            parameter.ParameterName = name;
            parameter.Value = value;
            reference.Parameters.Add(parameter);
        }

        /// <summary>
        /// Manages the misfire handling calls based on the misfire instruction.
        /// </summary>
        /// <param name="reference">The schedule builder to update.</param>
        /// <param name="misfireInstruction">The misfire instruction code.</param>
        public static void WithMisfireHandling(this CronScheduleBuilder reference, int misfireInstruction)
        {
            switch (misfireInstruction)
            {
                case MisfireInstruction.CronTrigger.DoNothing:
                    reference.WithMisfireHandlingInstructionDoNothing();
                    break;
                case MisfireInstruction.CronTrigger.FireOnceNow:
                    reference.WithMisfireHandlingInstructionFireAndProceed();
                    break;
                case MisfireInstruction.IgnoreMisfirePolicy:
                    reference.WithMisfireHandlingInstructionIgnoreMisfires();
                    break;
                case MisfireInstruction.SmartPolicy:
                    // Do nothing as this is the default choice
                    break;
                default:
                    string format = "The misfire instruction {0} is not managed by the cron manager";
                    string message = string.Format(format, misfireInstruction);
                    throw new ArgumentException(message, "misfireInstruction");
            }
        }
    }
}
